﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.InteropServices;
using myOffice = Microsoft.Office.Interop;
using System.Diagnostics;
using TpLogger;
using Microsoft.Office.Interop.Excel;
using TpBaseObjects.Base;

namespace TpTools {

    public class Office {

        

        public static myOffice.Word.Application GetWordApplication(Logger log) {
            try {
                Process[] procs = Process.GetProcesses();
                foreach (Process proc in procs) {
                    if (proc.ProcessName.IndexOf("WINWORD") > -1) {
                        return (myOffice.Word.Application)Marshal.GetActiveObject("Word.Application");
                    }
                }
                return new myOffice.Word.Application();
            } catch (Exception ex) {
                log.Add("GetWordApplication", ex);
            }
            return null;
        }

        

        public static Range GetAeRow(Worksheet sh, string projektName, string projektBezeichnung, int startRow) {
            bool startRowSelect = false;
            while (startRowSelect == false & startRow < 10000) {
                Range row = sh.Rows[startRow];
                AeRow aeRow = new AeRow(row);
                if (aeRow.ProjektName == projektName & aeRow.ProjektBezeichnung == projektBezeichnung) {
                    return row;
                }
                startRow++;
            }
            return null;
        }

        

        public static bool WordIsStarted(Logger log) {
            try {
                Process[] procs = Process.GetProcesses();
                foreach (Process proc in procs) {
                    if (proc.ProcessName.IndexOf("WINWORD") > -1) {
                        return true;
                    }
                }
            } catch (Exception ex) {
                log.Add("WordIsStarted", ex);
            }
            return false;
        }

        public static bool WorkbookIsLoaded(Microsoft.Office.Interop.Excel.Application exApp, string wbName) {
            foreach(Workbook wb in exApp.Workbooks) {
                if(wb.Name == wbName) {
                    return true;
                }
            }
            return false;
        }
    }
}

